<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\User;
use App\Http\Services\ContentCheckServices;
use App\Http\Services\MsgServices;
use App\Http\Services\TrendsServices;
use Illuminate\Http\Request;
use App\Lib\Response;

/*动态相关接口*/

class TrendsController extends BaseController
{
    protected $user;
    protected $service;
    protected $request;
    private $content_type = [1, 2, 3, 4, 6, 7, 8];//[1：纯文本  2：图文  3：视频  4：语音 5：CP 6:作品 7:发布web活动 8:活动分享img]
    private $trends_list_conf = [1, 2, 3, 4, 5, 6, 7, 8, 9,10];
    private $service_msg;
    protected $contentCheckServices;

    public function __construct(Request $request, User $user, TrendsServices $trendsServices, MsgServices $msgServices)
    {
        $this->service = $trendsServices;
        $this->user = $user;
        $this->request = $request;
//        $this->service->checkToken();
        $this->service_msg = $msgServices;
        $this->contentCheckServices = new ContentCheckServices();
    }

    /**
     * @desc 获取动态列表
     * @param int $act_type [获取类型]  [1:推荐； 2：最新； 3：附近； 4：声控； 5：关注； 6：粉丝 7：男孩 8：女孩 9：精选]
     * @param string $lng [用户所在经度]
     * @param string $lat [用户所在纬度]
     * */
    public function get_trends_list()
    {
        if(!$this->checkRequest()){
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $act_type = (int)$this->request->get('act_type');
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');
        $gender = 3;//(int)$this->request->get('gender');
        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;

        if (!$act_type || !in_array($act_type, $this->trends_list_conf)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $ret = $this->service->listService($act_type, $page, $page_size, $lng, $lat ,$gender);
        Response::returnResult(Hint::SUCCESS, $ret);
    }

    /**
     * @desc 获取视频动态列表
     * @param int $act_type [获取类型]  [1:推荐； 2：最新； 3：附近； 4：声控； 5：关注； 6：粉丝 7：男孩 8：女孩 9：精选]
     * @param string $lng [用户所在经度]
     * @param string $lat [用户所在纬度]
     * */
    public function get_trends_vide0_ist()
    {
//        if(!$this->checkRequest()){
//            Response::returnResult(Hint::LOGIN_OUT);
//        }

        $act_type = (int)$this->request->get('act_type');
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');
        $gender = 3;//(int)$this->request->get('gender');
        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size =5;
        $ctype=3;
        if (!$act_type || !in_array($act_type, $this->trends_list_conf)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $ret = $this->service->listService($act_type, $page, $page_size, $lng, $lat ,$gender,$ctype);
        Response::returnResult(Hint::SUCCESS, $ret);
    }
    public function get_trends()
    {

        $cid = (int)$this->request->get('cid');
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');
        if (empty($cid)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $ret = $this->service->get_trends($cid, $lat, $lng);
        if (empty($ret)){
            //动态找不到
            Response::returnResult(Hint::TRENDS_NOT_FOUND);
        }
        Response::returnResult(Hint::SUCCESS,$ret);
    }

    /**
     * 获取用户动态
     * */
    public function get_user_trends()
    {
        $user_id = (int)$this->request->get('user_id');
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');
        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;
        if (empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $ret = $this->service->get_user_trends($page, $page_size, $user_id, $lat, $lng);
        Response::returnResult(Hint::SUCCESS,$ret);
    }
    
    /**
     * @desc 发布动态
     * @param int $type [1：纯文本  2：图文  3：视频  4：语音 5：CP 6:作品 7:发布web活动]
     * @param string $content [文本内容]
     * @param string $resource_urls [资源]
     * @param string $position_name [位置名称]
     * @param $lng [经度]
     * @param $lat [纬度]
     * */
    public function trends_release()
    {
        $content = $this->request->post('content');
        $type = (int)$this->request->post('type');
        $resource_urls = explode(',', $this->request->post('resource_urls'));
        $resource_time = $this->request->post('resource_time');
        $position_name = $this->request->post('position_name');
        $lng = $this->request->post('lng');
        $lat = $this->request->post('lat');
        $cover =  explode(',', $this->request->post('cover'));
        $topicIds = $this->request->post('topic_ids', '');// 动态话题ID,逗号拼接（例如：1,2）
        $longImg = $this->request->post('long_img', 0);
	$handbookId = $this->request->post('handbook_id', 0);
	if(empty($content) || empty($this->request->post('resource_urls'))){
            Response::returnResult(Hint::NEED_CONTENT_AND_IMAGE);
        }
        if(!in_array($longImg, [0, 1, 2])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (!$type || !in_array($type, $this->content_type)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($type != 1 && !count($resource_urls)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        // 判断是否需要封面
        if (($type == 3 || $type == 4) && empty($cover)){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        // 判断资源播放时长是否存在
        if ($type == 4 && empty($resource_time)){
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $this->service->checkToken();
        $curUserInfo = null;
        $ret = $this->service->releaseService($type, $content, $resource_urls, $position_name, $lng, $lat, $cover, $resource_time, $topicIds, 0, $longImg, $curUserInfo, $handbookId);

        if ($ret === false) {
            Response::returnResult(Hint::ERROR);
        }
        $this->service_msg->trends_interaction($ret['content_id']);

        //添加动态内容检测
        $this->contentCheckServices->checkTrendContent($ret, $resource_urls);

        Response::returnResult(Hint::SUCCESS, $ret);
    }


    /**
     * @desc 删除动态
     * */
    public function trends_delete()
    {
        $content_id = (int)$this->request->post('content_id');
        if (!$content_id)
        Response::returnResult(Hint::PARAM_ERROR);

        $this->service->checkToken();

        $ret = $this->service->deleteService($content_id);
        if ($ret === false) {
            Response::returnResult(Hint::ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }
    
    /**
     * @desc 发布评论
     * @param int $content_id
     * @param string $comment_text
     * @param string $lng
     * @param string $lat
     * */
    public function trends_comment_release()
    {
        $content_id = (int)$this->request->post('content_id');
        $comment_text = $this->request->post('comment_text');//文本评论
        $voice_resource = $this->request->post('voice_resource');//语音内容
        $voice_time = $this->request->post('voice_time');//语音播放时长
        $lng = $this->request->post('lng');
        $lat = $this->request->post('lat');
        $comment_img = $this->request->post('comment_img') ?: ""; //图片,可选
        if (!$content_id) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $cur_user_id = null;
        $coRet = $this->service->commentService($content_id, $comment_text, $voice_resource, $lng, $lat, $voice_time, $cur_user_id,$comment_img);
        if ($coRet == Hint::AUTH_ERROR){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (!$coRet) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (!empty($cur_user_id)){
        //    $this->service_msg->trends_comments($content_id, $cur_user_id, $comment_text, $voice_resource);
        }

        //添加动态内容检测
        $this->contentCheckServices->checkTrendCommentContent($coRet);

        Response::returnResult(Hint::SUCCESS, $coRet);
    }
    
    /**
     * @desc 发布回复
     * @param int $reply_type [1:针对评论 2：针对回复]
     * @param int $comment_id [所属评论id]
     * @param int $reply_id [回复目标id  reply_type=1=comment_id  reply_type=2=回复的父回复id]
     * @param string $reply_text
     * @param string $voice_resource
     * */
    public function trends_reply_release()
    {
        $reply_type = (int)$this->request->post('reply_type');
        $content_id = (int)$this->request->post('content_id');
        $comment_id = (int)$this->request->post('comment_id');
        $reply_id = (int)$this->request->post('reply_id');
        $reply_text = $this->request->post('reply_text');//文本评论
        $voice_resource = $this->request->post('voice_resource');//语音内容
        $voice_time = $this->request->post('voice_time');//语音播放时长
        $reply_img = (string) $this->request->post('reply_img') ?: "";//图片,可选
        $lng = $this->request->post('lng');
        $lat = $this->request->post('lat');

        if (!$reply_type || !in_array($reply_type, [1, 2]) || !$comment_id || !$reply_id || !$content_id)
            Response::returnResult(Hint::PARAM_ERROR);

        if (!$reply_text && !$voice_resource)
            Response::returnResult(Hint::PARAM_ERROR);

        $lyRet = $this->service->replyService($reply_type, $comment_id, $reply_id, $reply_text, $voice_resource, $content_id, $lng, $lat, $voice_time,$reply_img);
        if ($lyRet == Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
            return;
        }
        if ($lyRet == Hint::USER_NOT_CERTIFICATION){
            Response::returnResult(Hint::USER_NOT_CERTIFICATION);
            return;
        }
        if (!$lyRet) {
            Response::returnResult(Hint::ERROR);
        }
        //添加动态内容检测
        $this->contentCheckServices->checkTrendCommentReplyContent($lyRet);
        Response::returnResult(Hint::SUCCESS, $lyRet);
    }
    
    /**
     * @desc 获取评论列表
     * @param int $content_id
     * @param string $lng
     * @param string $lat
     * */
    public function get_trends_comment_list()
    {
        $content_id = (int)$this->request->get('content_id');
        $lng = $this->request->get('lng');
        $lat = $this->request->get('lat');
        $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
        $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;

        if (!$content_id){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $list = $this->service->commentListService($content_id, $page, $page_size, $lng, $lat);
        Response::returnResult(Hint::SUCCESS,$list);
    }
    
    /**
     * @desc 获取回复列表
     * @param int $comment_id
     * */
    public function get_trends_reply_list()
    {

            $comment_id = (int)$this->request->get('comment_id');
            $lng = $this->request->get('lng');
            $lat = $this->request->get('lat');
            $page = !empty((int)$this->request->get('page_index')) && $this->request->get('page_index') >= 0 ? (int)$this->request->get('page_index') : 0;
            $page_size = !empty((int)$this->request->get('page_size')) && $this->request->get('page_size') > 0 ? (int)$this->request->get('page_size') : 10;
            
            if (!$comment_id)
                Response::returnResult(Hint::PARAM_ERROR);
        
                $list = $this->service->replyListService($comment_id, $page, $page_size, $lng, $lat);
            Response::returnResult(Hint::SUCCESS,$list);

    }


    /**
     * 删除回复
     */
    public function trends_reply_delete()
    {

            $reply_id = (int)$this->request->post('reply_id');
            if (!$reply_id)
                Response::returnResult(Hint::PARAM_ERROR);

            $this->service->checkToken();

            $ret = $this->service->deleteReplyService($reply_id);
            if ($ret === false) {
                Response::returnResult(Hint::ERROR);
            }
            Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 删除评论
     */
    public function trends_comment_delete()
    {
        $comment_id = (int)$this->request->post('comment_id');
        if (!$comment_id) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $ret = $this->service->deleteCommentService($comment_id);
        if ($ret === false) {
            Response::returnResult(Hint::ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }
    
    /**
     * @desc 动态内容点赞or取消点赞
     * @param int $act_type [操作类型] [1：点赞 0：取消点赞]
     * @param int $content_id [内容id]
     * */
    public function trends_like()
    {
        $act_type = (int)$this->request->post('act_type');
        $content_id = (int)$this->request->post('content_id');
        if (strlen($act_type) <= 0 || !in_array($act_type, [0, 1])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (!$content_id){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $this->service->checkToken();
        $cur_user_id = null;
        $this->service->likeService($act_type, $content_id, $cur_user_id);
        if ($act_type == 1 && !empty($cur_user_id)){
            $this->service_msg->trends_won($content_id, $cur_user_id);
        }
        Response::returnResult(Hint::SUCCESS);
    }
    
    /**
     * @desc 评论内容点赞or取消点赞
     * @param int $act_type [操作类型] [1：点赞 0：取消点赞]
     * @param int $comment_id [内容id]
     * */
    public function trends_comment_like()
    {
        $act_type = (int)$this->request->post('act_type');
        $comment_id = (int)$this->request->post('comment_id');
        if (strlen($act_type) <= 0 || !in_array($act_type, [0, 1]))
            Response::returnResult(Hint::PARAM_ERROR);

        if (!$comment_id)
            Response::returnResult(Hint::PARAM_ERROR);

        $this->service->checkToken();

        $this->service->likeCommentService($act_type, $comment_id);
        Response::returnResult(Hint::SUCCESS);
    }



    public function trends_reply_like()
    {
        $act_type = (int)$this->request->post('act_type');
        $reply_id = (int)$this->request->post('reply_id');
        if (strlen($act_type) <= 0 || !in_array($act_type, [0, 1]))
            Response::returnResult(Hint::PARAM_ERROR);

        if (!$reply_id)
            Response::returnResult(Hint::PARAM_ERROR);

        $this->service->checkToken();

        $this->service->likeReplyService($act_type, $reply_id);
        Response::returnResult(Hint::SUCCESS);
    }
    
    /**
     * @desc 提交内容阅读计数   不限制点赞时间
     * @param int $content_id [内容id]
     * */
    public function trends_read_release()
    {
        $content_id = (int)$this->request->post('content_id');
        if (!$content_id)
            Response::returnResult(Hint::PARAM_ERROR);

        $cur_user_id = null;
        $readService = $this->service->readService($content_id, $cur_user_id);
        if (!empty($cur_user_id)){
            $this->service_msg->trends_read($content_id, $cur_user_id);
        }
        $data['is_read'] = $readService;
        if($readService == false){
            $data['is_read'] = 0;
        }
        Response::returnResult(Hint::SUCCESS,$data);

    }
    
    /**
     * @desc 转发计数
     * */
    public function trends_relay_release()
    {
        $content_id = (int)$this->request->post('content_id');
        if (!$content_id)
            Response::returnResult(Hint::PARAM_ERROR);

        $this->service->relayService($content_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 屏蔽用户
     */
    public function blockUser()
    {
        $content_id = (int)$this->request->post('content_id');
        $user_id = (int)$this->request->post('user_id');
        if (empty($content_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $ret = $this->service->blockUser($content_id, $user_id);
        if (!$ret){
            Response::returnResult(Hint::AUTH_ERROR);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    ################# 后台调用 ##########################################

    /**
     * 动态内容更新
     * */
    public function trendsUpdate()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'trendsUpdate', $this->requestData);

        } while (false);

        $this->responseData();
    }


    /**
     * 删除评论
     * */
    public function commentDel()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'commentDel', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 动态审核开关
     * */
    public function trendsAuditSwitch()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'trendsAuditSwitch', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 设置动态类型
     * */
    public function setTrendsType()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'setTrendsType', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 动态审核
     * */
    public function trendsAudit()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $res = $this->callCommon($this->service, 'trendsAudit', $this->requestData ,false);
            if($res['code'] ==0 && $this->requestData['state'] ==1 && $res['data']['is_send_message'] ==1){
                $this->service_msg->trends_fans($this->requestData['content_id']);
            }else if($res['code'] ==0 && $this->requestData['state'] ==0 && !empty($res['data']['user_id'])){
                $this->service_msg->send_sys_text_msg($res['data']['user_id'],'您上传的动态未通过审核，请检查动态内容是否合规，有疑问可联系客服人员~');
            }
        } while (false);

        $this->responseData();
    }

    /**
     * 动态点赞列表
     * */
    public function trendsLikeList()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'trendsLikeList', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 打招呼
     */
    public function greet(){
        do {
            if (!$this->checkRequest()) {
                break;
            }
            $this->callCommon($this->service, 'greet', $this->requestData);
        } while (false);

        $this->responseData();
    }
    /**
     * 后台增加/修改 推荐广告位
     */
    public function curdTrendsAd(){
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'curdTrendsAd', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 后台删除 推荐广告位
     */
    public function delTrendsAd(){
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->service, 'delTrendsAd', $this->requestData);

        } while (false);

        $this->responseData();
    }
}
